﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Collections.Generic;
using System.Collections;
using System.Reflection;
using System.Linq;
using System.Web;
using System.Text;
using System.Security;
using System.Security.Cryptography;

/// <summary>
///User 的摘要说明
/// </summary>
public class User
{

    private static readonly string _connectionString = DataBaseClass.GetConnectionString();
    private int m_sID;//用户ID
    private string m_sUserName;//用户名
	private int m_nType;//用户类型

	public User(int nID, string  sUserName)
	{
		m_sID = nID;
		m_sUserName = sUserName;
		if (m_sID < 0)
			m_nType = 0;
		else
			m_nType = 1;
	}

    public int ID
    {
        get { return m_sID; }
    }

    public string UserName
    {
        get { return m_sUserName; }

    }
    
    public int Type
    {
        get { return m_nType; }
		set { m_nType = value; }

    }

	private static User GetUserByID(int nID)
	{
		DataTable table = DataBaseClass.selectData("SELECT ID,UserName FROM MovieUser WHERE ID=" + nID.ToString(), _connectionString);
		if (table.Rows.Count == 0)
			return null;
		return new User((int)table.Rows[0]["ID"], table.Rows[0]["UserName"] as string);
	}

	private static User GetUserByName(string sName)
	{
		string []s = {"ID" , "UserName"};
		DataTable table = DataBaseClass.selectData(s, "UserName='" + sName + "'", "MovieUser", _connectionString);
		//DataTable table = DataBaseClass.selectData("SELECT ID,UserName FROM User WHERE UserName='" + sName+"'", DataBaseHelper.Movie.GetConStr());
		if (table.Rows.Count == 0)
			return null;
		return new User((int)table.Rows[0]["ID"], table.Rows[0]["UserName"] as string);
	}
	
	public static User LogIn(string sName, string sPassword)
	{
		MD5 md5 = MD5.Create();//new MD5CryptoServiceProvider();
		byte[] sourse = System.Text.UnicodeEncoding.Default.GetBytes(sPassword);
		byte[] result = md5.ComputeHash(sourse);
		string sResult = "";
		for (int i = 0; i < result.Length; i++)
		{
			// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符
			sResult += Convert.ToString(result[i], 16).PadLeft(2, '0'); ;

		}
		sResult.PadLeft(32, '0');
		DataTable table = DataBaseClass.selectData("SELECT * FROM Password WHERE UserName='" + sName + "' AND Password='" + sResult + "'", _connectionString);
		if (table.Rows.Count == null)
			return null;
		DataBaseClass.Update("Update MovieUser SET IsLogIn=1 WHERE UserName='" + sName + "'", _connectionString);
		return   GetUserByName(sName);
		
	}

	public static User LogOut(int nID)
	{
		DataBaseClass.Update("Update MovieUser SET IsLogIn=0 WHERE ID=" + nID, _connectionString);
		return new User(-1 , "未登录");
	}

	public User LogOut()
	{
		DataBaseClass.Update("Update MovieUser SET IsLogIn=0 WHERE ID=" + m_sID, _connectionString);
		this.m_sID = -1;
		this.m_sUserName = "未登录";
		this.m_nType = 0;
		return this;
	}
    //1)获得信息修改时的用户对象,判断用户类型，是本人时返回一个UserModify的对象
    /// <summary>
    /// 获得信息修改时的用户对象,通过ID是否匹配判断，匹配则返回UserModify类型对象，否则返回null
    /// </summary>
    /// <param name="用户ID"></param>
    /// <returns></returns>
    public UserModify GetUserModify()
    {
		if(this.m_nType != 1)
			return null;
		String sql = "SELECT * FROM MovieUser WHERE ID="+this.m_sID;
		DataTable dataTable = DataBaseClass.selectData(sql, DataBaseClass.GetConnectionString());
		if (dataTable.Rows.Count == 0)
			return null;
		return UserModify.GetUserByRow(dataTable.Rows[0]);
        //新建UserModify类
    //    UserModify newUserModify = new UserModify(ID, userName, type, dateReg, dateBirthday, sex, email, TelNumber);
        //判断返回，如果匹配就返回对象
		//if (newUserModify.ID == UserID)
		//    return newUserModify;
		//else return null;

    }
	public DataTable GetDataTableOfCommentOfMovie()
	{
		return GetDataTableOfCommentOfMovie(this.m_sID);
	}
    //5)获得某人对所有电影的评论列表
    /// <summary>
    /// 获得某人对电影的所有评论，返回DataTable类型对象，通过ID查找用户
    /// </summary>
    /// <param name="用户ID"></param>
    /// <returns></returns>
    public static DataTable GetDataTableOfCommentOfMovie(int UserID)
    {
        //要选择的列和查询条件
        
        //返回DataTable型对象
		return DataBaseClass.selectData("SELECT * FROM M_Comment WHERE UserID="+UserID , _connectionString);
    }

	public DataTable GetDataTableOfCommentOfTheatre()
	{
		return GetDataTableOfCommentOfTheatre(this.m_sID);
	}
    //6)获得某人对所有电影院的评论
    /// <summary>
    /// 获得某用户对电影院的所有评论，返回DataTable类型对象，通过ID查找用户
    /// </summary>
    /// <param name="用户ID"></param>
    /// <returns></returns>
    public static DataTable GetDataTableOfCommentOfTheatre(int UserID)
    {
		return DataBaseClass.selectData("SELECT * FROM T_Comment WHERE UserID=" + UserID, _connectionString);
        //要选择的列和查询条件
        string[] att = { "ID" , "UseID", "TheaterID", "Date", "Title", "Detail" };
        string condition = "ID=" + UserID.ToString();

        //返回DataTable型对象
        DataTable m_dtbTComment = DataBaseClass.selectData(att, condition, "T_Comment", _connectionString);
        return m_dtbTComment;
    }

	public DataTable GetDataTableOfLetter()
	{
		return GetDataTableOfLetter(this.m_sID);
	}
    //7)获得某人收到的所有站内信
    /// <summary>
    /// 获得某人收到的所有站内信，返回DataTable类型对象，通过收信人ID查找用户
    /// </summary>
    /// <param name="收信人ID"></param>
    /// <returns></returns>
    public static DataTable GetDataTableOfLetter(int recieverID)
    {
		return DataBaseClass.selectData("SELECT * FROM Mail WHERE RecieverID=" + recieverID, _connectionString);
        //要选择的列和查询条件
    }
	public DataTable GetDataTableOfFavorites()
	{
		return GetDataTableOfFavorites(this.m_sID);
	}
	public static  DataTable GetDataTableOfFavorites(int userID)
	{
		return DataBaseClass.selectData("SELECT * FROM Favourites WHERE userID=" + userID, _connectionString);
	}
	public DataTable GetDataTableOfOrder()
	{
		return GetDataTableOfOrder(this.m_sID);
	}
	public static  DataTable GetDataTableOfOrder(int userID)
	{
		return DataBaseClass.selectData("SELECT * FROM Order WHERE userID=" + userID, _connectionString);
	}
    //public List<User> GetListFromDataTable(DataTable dt)
    //{
    //    List<User> temp = new List<User>();
    //    for (int i = 0; i < dt.Rows.Count; i++)
    //    {
    //        User user = new User();
    //        temp.Add(user);
    //    }
    //    return temp;
    //}
  
}